#include <SypX/SypX.h>

#ifdef SYPX_MEMCHECK
#include "mmgr.h"
#endif

#include "IntroScene.h"
#include "MenuScene.h"
#include "GameScene.h"
#include "SheepDefenceApp.h"



namespace SheepDefence
{
	IntroScene::IntroScene(SypX::Font* f)
	{
		font = f;
	}

	IntroScene::~IntroScene(void)
	{
		delete logo;
		delete txtBox;
		engine->getAudioEngine()->deleteSoundBuffer(snd);

	}

	void IntroScene::startGame(SypX::GuiComponent* g)
	{
		std::cout<<g->getFileName()<<"\n";
	}

	void IntroScene::init()
	{		
		engine = SypX::Engine::getSingleton();
		render = engine->getRenderer();
		canvas = engine->getCanvas();	
		if(!font)
		{
			font = engine->loadFontFromFile("Fonts/sheepFont.ttf", 24, 4);
		}

		logoMesh = new SypX::Mesh();
		logoMesh->initAsQuad(256.0f,128.0f);

		logoTex = SypX::Texture::loadFromFile("Textures/sega.png");

		logo = new SypX::MeshInstance();
		logo->useMesh(logoMesh);
		logo->useTexture(logoTex);
		logo->setPosition(SypX::Vec2(160.0f, 240.0f));		

		txtBox = new SypX::TextBox(font, 100.0f, 30.0f, 4, 0);		
		txtBox->getMeshInstance()->setPosition(SypX::Vec2(100.0f, 150.0f));
		txtBox->setBgColor(SypX::Vec4(0.4f,0.4f,0.4f,1.0f));
		txtBox->setTextColor(SypX::Vec4(0.0f,1.0f,0.4f,1.0f));
		txtBox->setBorderColor(SypX::Vec4(1.0f,0.4f,0.4f,1.0f));
		txtBox->setText("WOOO");

		render->setClearScreenColor(SypX::Vec4(0.0f, 0.0f,0.0f,1.0f));		

		snd = engine->getAudioEngine()->loadFromWAVFile("Sounds/Sega.wav");
		//engine->getAudioEngine()->playSound(snd, SypX::Vec3()) ;		
		totalTime = 0;


	}

	void IntroScene::handleInput()
	{
		SypX::Event e = canvas->getEvent();
	}

	void IntroScene::run()
	{
		render->clearScreen();		
		font->drawText("Hello World!\n Testing", 20.0f, 50.0f);
		render->setActiveColor(SypX::Vec4(1.0f, 0, 0, 1.0f));
		font->drawBufferedInstances();
		render->setActiveColor(SypX::Vec4(1.0f, 1.0f, 1.0f, 1.0f));
		render->drawMesh(logo);
		
		txtBox->draw();
		handleInput();
		totalTime += canvas->getElapsedTime();		
		if(totalTime >= 3.0f)
		{
			changeScene(new MenuScene(font));			
		}		
		canvas->swapBuffers();
	}

}
